package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.pojo.Feedback;
import com.itheima.service.FeedbackService;
import com.itheima.utils.Result;
import com.itheima.utils.StatusCode;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/feedback")
public class FeedbackController {

    @Resource
    private FeedbackService feedbackService;

    /**
     * 批量删除反馈
     * @Param List<Feedback>
     * */
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Feedback> feedbackList){
        Integer count = 0;
        if(feedbackList != null && feedbackList.size() > 0){
            for (Feedback feedback : feedbackList) {
                count = feedbackService.delete(feedback);
            }
        }
        if (count > 0) {
            return new Result<>(StatusCode.SUCCESS, "删除成功");
        } else {
            return new Result<>(StatusCode.FAIL, "删除失败");
        }
    }

    /**
     * 编辑反馈
     * @Param Feedback
     * */
    @PostMapping("/edit")
    public Result edit(@RequestBody Feedback feedback){
        Integer count = feedbackService.edit(feedback);
        if (count > 0) {
            return new Result<>(StatusCode.SUCCESS, "更新成功");
        } else {
            return new Result<>(StatusCode.FAIL, "更新失败");
        }
    }

    /**
     * 获取反馈列表
     * @Param Feedback
     * */
    @PostMapping("/getList")
    public Result getList(@RequestBody Feedback feedback){
        Page<Feedback> feedbacks = feedbackService.getList(feedback);

        Map<String,Object> map = new HashMap<>();
        map.put("records",feedbacks.getResult());
        map.put("total",feedbacks.getTotal());
        map.put("size",feedbacks.getPageSize());
        map.put("current",feedbacks.getPageNum());
        map.put("orders",null);
        map.put("optimizeCountSql",true);
        map.put("isSearchCount",true);

        return new Result<>(StatusCode.SUCCESS, map);
    }

}
